/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.jetty;

import org.apache.tools.ant.types.resources.FileResource;

import org.mortbay.jetty.webapp.WebAppClassLoader;
import org.mortbay.jetty.webapp.WebXmlConfiguration;

import org.mortbay.resource.Resource;

import org.mortbay.xml.XmlConfiguration;

import java.io.IOException;

import java.net.URL;

import java.util.Iterator;


/**
 *
 */
public class StandardConfiguration extends WebXmlConfiguration {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    private URL jettyEnvXmlUrl;

    //~ Constructors ---------------------------------------------------------------------------------------------------

    public StandardConfiguration(URL jettyEnvXmlUrl) {
        this.jettyEnvXmlUrl = jettyEnvXmlUrl;
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    public void configureClassLoader() throws Exception {
        if (getContext().getClassPathFiles() != null) {
            for (Iterator i = getContext().getClassPathFiles().iterator(); i.hasNext();) {
                FileResource fileResource = (FileResource)i.next();
                WebAppClassLoader loader = (WebAppClassLoader)getWebAppContext().getClassLoader();
                loader.addClassPath(fileResource.getFile().getCanonicalPath());
            }
        }
    }

    protected URL findWebXml() throws IOException {
        // If an explicit web.xml file has been set (eg for jetty:run) then use it
        if ((getContext().getWebXmlFile() != null) && getContext().getWebXmlFile().exists()) {
            return getContext().getWebXmlFile().toURI().toURL();
        }

        // Uf we haven't overridden location of web.xml file, use the standard way of finding it
        return super.findWebXml();
    }

    private PluginWebAppContext getContext() {
        return (PluginWebAppContext)getWebAppContext();
    }

    public void configureWebApp() throws Exception {
        super.configureWebApp();

        // Check to see if an explicit file has been set, if not, look in WEB-INF/jetty-env.xml
        if (jettyEnvXmlUrl == null) {
            // Look for a file called WEB-INF/jetty-env.xml and process it if it exists
            Resource webInf = getWebAppContext().getWebInf();

            if ((webInf != null) && webInf.isDirectory()) {
                Resource jettyEnv = webInf.addPath("jetty-env.xml");

                if (jettyEnv.exists()) {
                    jettyEnvXmlUrl = jettyEnv.getURL();
                }
            }
        }

        if (jettyEnvXmlUrl != null) {
            new XmlConfiguration(jettyEnvXmlUrl).configure(getWebAppContext());
        }
    }
}
